package com.csu.panel;

import com.csu.consts.UiConsts;
import com.csu.ui.LoginUI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Date;

public class UserPanel_Right extends JPanel {
    JLabel label_profile_photo;
    JLabel label_welcome;
    JLabel label_account;
    JTextField textField_username;
    ImageIcon icon_confirm;
    ImageIcon icon_cancel;
    JLabel button_confirm;
    JLabel button_cancel;

    public UserPanel_Right(){
        setLayout(null);
        setBackground(Color.WHITE);

        icon_confirm = new ImageIcon("photo/confirm.png");
        icon_confirm.setImage(icon_confirm.getImage().getScaledInstance(25,25,Image.SCALE_DEFAULT));
        icon_cancel = new ImageIcon("photo/cancel.png");
        icon_cancel.setImage(icon_cancel.getImage().getScaledInstance(25,25,Image.SCALE_DEFAULT));

        label_profile_photo = new JLabel();
        label_welcome = new JLabel();
        label_account = new JLabel();
        textField_username = new JTextField();
        button_confirm = new JLabel(icon_confirm);
//        button_confirm.setHorizontalAlignment(SwingConstants.CENTER);
//        button_confirm.setVerticalAlignment(SwingConstants.CENTER);
        button_cancel = new JLabel(icon_cancel);

        ImageIcon image = new ImageIcon("photo/user_profile_photo.png");
        image.setImage(image.getImage().getScaledInstance(115,115,Image.SCALE_DEFAULT));
        label_profile_photo = new JLabel(image);
        label_profile_photo.setBounds( 85, 0, 115, 115 );
        label_profile_photo.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                LoginUI.mainUI.showUser();
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });

        label_account.setBounds(100, 135, 85, 25);
        label_account.setFont(UiConsts.FONT_SUB_TITLE);
        label_account.setHorizontalAlignment(JLabel.CENTER);
        label_account.setText("用户名");
        textField_username.setBounds(100, 130, 85, 35);
        textField_username.setBorder(new RoundBorder());
        textField_username.setVisible(false);
        textField_username.setFont(UiConsts.FONT_INSIDE_TEXTFIELD);
        textField_username.setHorizontalAlignment(JTextField.CENTER);
        button_confirm.setBounds(200, 135, 25, 25 );
        button_cancel.setBounds(60, 135, 25, 25 );
        button_confirm.setVisible(false);
        button_cancel.setVisible(false);
        label_welcome.setBounds( 0, 170, 285, 25 );
        label_welcome.setFont(UiConsts.FONT_SUB_TITLE);
        label_welcome.setHorizontalAlignment(JLabel.CENTER);
        //label_welcome.setText("您好");
        int hour =new Date().getHours();
        if(hour>6 && hour<=12) {
            label_welcome.setText("上午好");
        }
        else if(hour>12 && hour<=18) {
            label_welcome.setText("下午好");
        }
        else {
            label_welcome.setText("晚上好");
        }
        label_welcome.requestFocus();

        add(label_profile_photo);
        add(label_welcome);
        add(textField_username);
        add(label_account);
        add(button_cancel);
        add(button_confirm);

        label_account.addMouseListener(new MouseListener(){
            public void mouseClicked(MouseEvent e) {
                // 处理鼠标点击
                if(e.getClickCount() == 2){
                    textField_username.setVisible(true);
                    textField_username.setEnabled(true);
                    textField_username.setText(label_account.getText());
                    textField_username.requestFocus();
                    textField_username.selectAll();
                    button_cancel.setVisible(true);
                    button_cancel.setEnabled(true);
                    button_confirm.setVisible(true);
                    button_confirm.setEnabled(true);
                }
            }

            public void mouseEntered(MouseEvent e) {
                // 处理鼠标移入
            }
            public void mouseExited(MouseEvent e) {
                // 处理鼠标离开
            }
            public void mousePressed(MouseEvent e) {
                // 处理鼠标按下
            }
            public void mouseReleased(MouseEvent e) {
                // 处理鼠标释放
            }
        });

        button_confirm.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                label_account.setText(textField_username.getText());
                textField_username.setVisible(false);
                textField_username.setEnabled(false);
                button_cancel.setVisible(false);
                button_cancel.setEnabled(false);
                button_confirm.setVisible(false);
                button_confirm.setEnabled(false);
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });

        button_cancel.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                textField_username.setVisible(false);
                textField_username.setEnabled(false);
                button_cancel.setVisible(false);
                button_cancel.setEnabled(false);
                button_confirm.setVisible(false);
                button_confirm.setEnabled(false);
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });
    }

}
